package com.kiwilss.aopdemo.ui;

import android.util.Log;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.kiwilss.aopdemo.utils.LogUtils;

public class WorkUtil implements LifecycleObserver {
 
    
    private static final String TAG = "MMM";
 
    private boolean whetherToCount = true;
 
    private int count = 0;
 
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void start() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (whetherToCount) {
                    try {
                        Thread.sleep(1000);
                        count++;
                        Log.d(TAG, "start: " + count);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
 
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop() {
        whetherToCount = false;
        Log.d(TAG, "onStop: " + count);
    }
 
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy() {
        LogUtils.e(count,"onDestroy");
    }
}
 